package Deprecated;

import com.lcfgly.Exception.SQLEmptyException;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 数据设置辅助类
 * @author lcfgly
 */
public class SetData implements Serializable {
    
    /**
     * 自动赋值 JavaBean
     * @param data 数据集合
     * @param bean 设置的对象
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     * @throws InvocationTargetException 
     */
    @Deprecated
    public final static void setBean(Map data,Object bean) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
    {
        Class<? extends Object> c=bean.getClass();
        Method[] methods=c.getMethods();
        int methodscount=methods.length;
        for(int i=0;i<methodscount;i++)
        {
            String name=methods[i].getName();
            if(name.startsWith("set"))
            {
                Class<?>[] cc=methods[i].getParameterTypes();
                if(cc.length ==1)
                {
                    String type=cc[0].getName();
                    String prop= Character.toLowerCase(name.charAt(3)) + name.substring(4);
                    Object param=data.get(prop);
                    if(param != null && param != "")
                    {
                        if(type.equals("java.lang.String")) {
                            methods[i].invoke(bean, new Object[] {param});
                        }
                        else if(type.equals("int") || type.equals("java.lang.Integer")) {
                            methods[i].invoke(bean, new Object[] {Integer.parseInt(param.toString())});
                        }else if(type.equals("long") || type.equals("java.lang.Long")) {
                            methods[i].invoke(bean, new Object[] {Long.parseLong(param.toString())});
                        } else if(type.equals("boolean") || type.equals("java.lang.Boolean")) {
                            methods[i].invoke(bean, new Object[] {Boolean.parseBoolean(param.toString())});
                        }else if(type.equals("short") || type.equals("java.lang.Short")){
                            methods[i].invoke(bean, new Object[]{Short.parseShort(param.toString())});
                        }else {
                            methods[i].invoke(bean, new Object[]{param});
                        }
                    }
                }
            }
        }
    }
    
    /**
     * 自动设置Bean
     * @param <T>
     * @param data
     * @param object
     * @return
     * @throws SQLException 
     */
    @Deprecated
    public final static <T> T setBean(Map data,Class<T> object) throws SQLException
    {
        try {
            T result=object.newInstance();
            Field[] f=object.getDeclaredFields();
            for(Field x:f)
            {
                String paramname=x.getName();
                String type=x.getType().getName();
                if(!paramname.equals("serialVersionUID"))
                {
                    x.setAccessible(true);
                    if(type.equals("int") || type.equals("java.lang.Integer")) {
                            x.set(result,Integer.parseInt(data.get(paramname).toString()));
                    }else if(type.equals("long") || type.equals("java.lang.Long")) {
                            x.set(result,(Long)data.get(paramname));
                    } else if(type.equals("boolean") || type.equals("java.lang.Boolean")) {
                        x.set(result,(Boolean)data.get(paramname));
                    }else if(type.equals("short") || type.equals("java.lang.Short")){
                        x.set(result,Short.parseShort(data.get(paramname).toString()));
                    }else {
                        x.set(result,data.get(paramname)); 
                    }
                }
            }
            return result;
        } catch (InstantiationException ex) {
            throw new SQLEmptyException(ex);
        } catch (IllegalAccessException ex) {
            throw new SQLEmptyException(ex);
        }
    }
    
    /**
     * 设置参数
     * @param arg0 参数集合
     * @param prepstmt  设置的PreparedStatement对象
     * @throws SQLException
     */
    @Deprecated
    public final static void setparams(List arg0,PreparedStatement prepstmt) throws SQLException
    {
        int i=1;
        for (Object object : arg0) {
            if(object == null){
                prepstmt.setNull(i++,Types.NULL);
            }else if(object instanceof Integer){
                 prepstmt.setInt(i++, (Integer)object);
            }else if(object instanceof String){
                prepstmt.setString(i++, (String)object);
            }else if(object instanceof Double){
                prepstmt.setDouble(i++, (Double)object);
            }else if(object instanceof Long){
                prepstmt.setLong(i++, (Long)object);
            }else if(object instanceof Float){
                prepstmt.setFloat(i++, (Float)object);
            }else if(object instanceof Short){
                prepstmt.setShort(i++, (Short)object);
            }else if(object instanceof  Boolean){
                prepstmt.setBoolean(i++, (Boolean)object);
            }else{
                prepstmt.setString(i++, object.toString());
            }
        }
    }
    
    /**
     * 动态设置插入值
     * @param object 取值的对象（遍历对象能set的所有值）
     * @param key 值的名称
     * @param value 对应"?"字符
     * @param objects  对应值存放对象
     */
    public final static void getInertparam(Object object,StringBuffer key,StringBuffer value,ArrayList<Object> objects)
    {
        Class c=object.getClass();
        Method[] ms=c.getMethods();
        for (Method method : ms) {
            String methodname=method.getName();
            if(methodname.startsWith("set"))
            {
                String paramname=Character.toLowerCase(methodname.charAt(3))+methodname.substring(4);
                Object param=null;
                try {
                    param=c.getMethod("get"+methodname.substring(3)).invoke(object, new Object[]{});
                } catch (NoSuchMethodException ex) {
                    Logger.getLogger(SetData.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SecurityException ex) {
                    Logger.getLogger(SetData.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(SetData.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalArgumentException ex) {
                    Logger.getLogger(SetData.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InvocationTargetException ex) {
                    Logger.getLogger(SetData.class.getName()).log(Level.SEVERE, null, ex);
                }
                if(param != null && !param.equals("id") )
                {
                    key.append(paramname);
                    key.append(",");
                    value.append("?,");
                    objects.add(param);
                }
            }
         }
    }
    
    /**
     * 设置UPDATE数据
     * @param object 取值的对象（遍历对象能set的所有值）
     * @param sql  查询sql数据字符串
     * @param objects 对应数据存放对象
     */
    public final static void getUpdateparam(Object object,StringBuffer sql,ArrayList<Object> objects)
    {
        Class c=object.getClass();
        Method[] ms=c.getMethods();
        for (Method method : ms) {
            String methodname=method.getName();
            if(methodname.startsWith("set"))
            {
                String paramname=Character.toLowerCase(methodname.charAt(3))+methodname.substring(4);
                Object param=null;
                try {
                    param=c.getMethod("get"+methodname.substring(3)).invoke(object, new Object[]{});
                } catch (NoSuchMethodException ex) {
                    Logger.getLogger(SetData.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SecurityException ex) {
                    Logger.getLogger(SetData.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(SetData.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalArgumentException ex) {
                    Logger.getLogger(SetData.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InvocationTargetException ex) {
                    Logger.getLogger(SetData.class.getName()).log(Level.SEVERE, null, ex);
                }
                sql.append( paramname);
                sql.append("=?,");
                objects.add(param);
            }
         }
    }
    
}
